using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Memorandum.DBContext;
using Memorandum.Models;
using Memorandum.Service;
using Memorandum.Views;
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Memorandum.ViewModel
{
    public class Message
    {
        public static readonly string ShowWait = nameof(ShowWait);
        public static readonly string HiddenWait = nameof(HiddenWait);
        public static readonly string ShowSuccess = nameof(ShowSuccess);
    }
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private INavigationService INav;
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(INavigationService srvice)
        {
            INav = srvice;

            Task.Run(() =>
            {
                ShowWait("");
                var items = SqliteDBContext.Instance.RemindItems;
                HiddenWait("");
            });
            //Page1
            //views/Home.xaml
            this.DisplayPage =INav.Navigate(new Uri("views/Home.xaml", UriKind.Relative));

            Messenger.Default.Register<string>(this, Message.ShowWait, ShowWait);
            Messenger.Default.Register<string>(this, Message.HiddenWait, HiddenWait);
            Messenger.Default.Register<string>(this, Message.ShowSuccess, ShowSuccess);
        }

        private void ShowSuccess(string obj)
        {
            if (SucVS != Visibility.Visible)
            {
                SucVS = Visibility.Visible;
            }
        }

        private void ShowWait(string obj)
        {
            
            if(WaitVS!=Visibility.Visible)
            {
                WaitVS = Visibility.Visible;
            }
        }
        private void HiddenWait(string obj)
        {

            if (WaitVS != Visibility.Collapsed)
            {
                WaitVS = Visibility.Collapsed;
            }

        }
       
        private Visibility sucVS = Visibility.Collapsed;
        public Visibility SucVS
        {
            get { return sucVS; }
            set
            {
                Set(ref sucVS, value);
            }
        }

        private Visibility waitVS=Visibility.Collapsed;

        public Visibility WaitVS
        {
            get { return waitVS; }
            set
            { 
               Set(ref waitVS ,value);
            }
        }


        private Uri displayPage;

        public Uri DisplayPage
        {
            get
            {
                return displayPage;
            }
            set
            {
                if (displayPage == value)
                {
                    return;
                }
                Set(ref displayPage, value);
            }
        }

        private ICommand _operatorViewMenuCommand;
        public ICommand GoHome
        {
            get
            {
                if (_operatorViewMenuCommand == null)
                {
                    _operatorViewMenuCommand = new RelayCommand(ProcessNavRequest);
                }
                return _operatorViewMenuCommand;
            }
        }
        
        private void ProcessNavRequest()
        {
           
            this.DisplayPage =INav.Navigate(new Uri("views/Home.xaml", UriKind.Relative));
        }
        private ICommand closeSuccess;

        public ICommand CloseSuccess
        {
            get
            {
                if (closeSuccess == null)
                {
                    closeSuccess = new RelayCommand(() => {

                        SucVS = Visibility.Collapsed;
                    },
                    () => {
                        return true;
                    });
                }
                return closeSuccess;
            }

        }

        private ICommand goBackCommand;

        public ICommand GoBackCommand
        {
            get 
            {
                if (goBackCommand == null)
                {
                    goBackCommand = new RelayCommand(() => {
                       
                        //  var vm = ServiceLocator.Current.GetInstance<MainViewModel>();
                        if(INav.CanGoBack())
                        {
                            this.DisplayPage = INav.GoBack();
                        }
                    },
                    ()=> {
                        return true;
                    });
                }
                return goBackCommand;
            }
            
        }

        private ICommand goForwardCommand;

        public ICommand GoForwardCommand
        {
          
            get
            {
                if (goForwardCommand == null)
                {
                    goForwardCommand = new RelayCommand(() => {
                        // var vm = ServiceLocator.Current.GetInstance<MainViewModel>();
                       if(INav.CanGoForward())
                        {
                            this.DisplayPage = INav.GoForward();
                        }  
                    }, 
                    ()=> {
                        return true;
                    });
                }
                return goForwardCommand;
            }
         
        }
    }
}